// subpackage/pages/article/index.js
import requestUtil from '../../baseRequest'
import requestUrls from '../../requestUrl'
import { $Message } from '../../components/base/index'
let h;
let w;
wx.getSystemInfo({
  success: function (res) {
    h = res.windowHeight - 30
  },
})
Page({

  /**
   * 页面的初始数据
   */
  data: {
    maxHeight:h,
    queryParams: {
      openId:wx.getStorageSync('openid'),
      pageDto: {
        pageNum: 1,
        pageSize: 5
      },
    },
    total:0,
    showList:[],
    prefixUrl :requestUrls.baseImagePrefixUrl
  },
  getShowPage(){
    requestUtil.postRequest(requestUrls.article.getPage, this.data.queryParams, (res) => {
      if(res){
        if(this.data.queryParams.pageDto.pageNum===1){
          this.setData({
            showList:[]
          })
        }
        this.setData({
          showList:this.data.showList.concat(res.list),
          total:res.total
        })
      }
    })
  },
  toDetail(event){
    console.log(event);
    let id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/article/detail?id='+id,
    })
  },
  toNextPage(event){
    let pageNum = this.data.queryParams.pageDto.pageNum;
    if(5*(pageNum+1)>this.data.total){
      this.showErrorMessage("已加载全部数据");
    }else{
      this.setData({
        queryParams: {
          openId:wx.getStorageSync('openid'),
          pageDto: {
            pageNum: pageNum+1,
            pageSize: 5
          },
        },
      })
      this.getShowPage();
    }
  },
  showErrorMessage(msg) {
    $Message({
      content: msg,
      type: 'error'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getShowPage();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getShowPage();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})